﻿package com.plter.controls.asmax
{
	import com.plter.events.AFormEvent;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * ...
	 * @author Jaja as-max.cn
	 */
	public class AForm extends ASprite
	{
		/**
		 * AForm窗体正在关闭时触发此事件
		 * @eventType	cn.asmax.events.AFormEvent.AFORM_CLOSING
		 */
		[Event(name = "aformClosing", type = "com.plter.events.AFormEvent")]
		
		/**
		 * AForm窗体关闭后触发此事件
		 * @eventType	cn.asmax.events.AFormEvent.AFORM_CLOSE
		 */
		[Event(name = "aformClose", type = "com.plter.events.AFormEvent")]
		
		
		private var dragStartThisX:Number = 0;
		private var dragStartThisY:Number = 0;
		private var dragStartMouseX:Number = 0;
		private var dragStartMouseY:Number = 0;
		
		/**
		 * 窗体的关闭按钮
		 */
		private var shut:AShutButton = new AShutButton;
		
		/**
		 * 窗体是否可以关闭,默认为可以关闭
		 */
		private var isShutable:Boolean = true;
		
		/**
		 * 窗体是否可以最大化,默认为可以最大化
		 */
		private var isMaxable:Boolean = true;
		
		/**
		 * 标题栏
		 */
		private var titleBar:ATitleBar = new ATitleBar;
		
		private var notifyTimer:Timer = new Timer(60, 6);
		
		/**
		 * 背景是否为通知模式
		 */
		private var isNotifyBG:Boolean = false;
		
		private var maxBox:AMaxBox = new AMaxBox;
		
		//通常模式下的宽高
		private var normalSizeWidth:Number = 320;
		private var normalSizeHeight:Number = 240;
		
		//通常模式下窗体的位置
		private var normalSizeX:Number = 0;
		private var normalSizeY:Number = 0;
		
		/**
		 * 是否可被拖动
		 */
		private var _asDragable:Boolean = true;
		
		/**
		 * 用户设置的是否可被拖动
		 */
		private var _dragable:Boolean = true;
		
		/**
		 * 标题栏的颜色
		 */
		private var _titleBarFillColor:uint = 0xCCCCCC;
		
		/**
		 * 标题栏边框的颜色
		 */
		private var _titleBarLineColor:uint = 0xFFFFFF;
		
		public function AForm() :void
		{
			//绘制背景
			drawBG();
			
			//标题栏
			titleBar.x = 2;
			titleBar.y = 2;
			titleBar.doubleClickEnabled = true;
			addChild(titleBar);
			titleBar.addEventListener(MouseEvent.MOUSE_DOWN, mouseEventHandler);
			titleBar.addEventListener(MouseEvent.DOUBLE_CLICK, mouseEventHandler);
			
			//添加按钮
			addChild(shut);
			shut.addEventListener(MouseEvent.CLICK, mouseEventHandler);
			shutable = true;
			
			addChild(maxBox);
			maxBox.addEventListener(MouseEvent.CLICK, mouseEventHandler);
			maxable = true;
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownThis);
		}
		
		/**
		 * 指示窗体是否可以关闭
		 */
		public function set shutable(value:Boolean):void {
			isShutable = value;
			shut.visible = value;
			fixButtonsLayout();
		}
		/**
		 * 指示窗体是否可以关闭
		 */
		public function get shutable():Boolean {
			return isShutable;
		}
		
		/**
		 * 指示窗体是否可以最大化
		 */
		public function set maxable(value:Boolean):void {
			isMaxable = value;
			maxBox.visible = isMaxable;
			
			if (value) {
				if (!titleBar.hasEventListener(MouseEvent.DOUBLE_CLICK)) {
					titleBar.addEventListener(MouseEvent.DOUBLE_CLICK, mouseEventHandler);
				}
			}else {
				titleBar.removeEventListener(MouseEvent.DOUBLE_CLICK, mouseEventHandler);
			}
			
			fixButtonsLayout();
		}
		/**
		 * 指示窗体是否可以最大化
		 */
		public function get maxable():Boolean {
			return isMaxable;
		}
		
		/**
		 * 设置窗体标题
		 */
		public function set title(value:String):void {
			titleBar.title = value;
		}
		/**
		 * 获得窗体标题
		 */
		public function get title():String {
			return titleBar.title;
		}
		
		/**
		 * 通知用户此窗体正在使用
		 */
		public function notify():void {
			notifyTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerEventHandler);
			notifyTimer.addEventListener(TimerEvent.TIMER, timerEventHandler);
			notifyTimer.reset();
			notifyTimer.start();
		}
		
		/**
		 * 指示此窗体是否可以被拖动
		 */
		public function set dragable(value:Boolean):void {
			_dragable = value;
			
			asDragable = value;
		}
		/**
		 * 指示此窗体是否可以被拖动
		 */
		public function get dragable():Boolean {
			return _dragable && _asDragable;
		}
		
		/**
		 * 设置窗体的大小
		 * @param	wid
		 * @param	hei
		 */
		public function setSize(wid:Number, hei:Number):void {
			drawBG(wid, hei, bgFillColor, bgLineColor);
			titleBar.drawBG(wid - 4, titleBar.bgHeight, titleBar.bgFillColor, titleBar.bgLineColor);
			fixButtonsLayout();
		}
		
		/**
		 * 指示窗体的宽度
		 */
		public override function get width():Number { return super.width; }
		/**
		 * 指示窗体的宽度
		 */
		public override  function set width(value:Number):void 
		{
			drawBG(value, bgHeight, bgFillColor, bgLineColor);
			titleBar.drawBG(value-4, titleBar.bgHeight, titleBar.bgFillColor, titleBar.bgLineColor);
			fixButtonsLayout();
		}
		
		/**
		 * 指示窗体的高度
		 */
		public override function get height():Number { return super.height; }
		/**
		 * 指示窗体的高度
		 */
		public override  function set height(value:Number):void 
		{
			drawBG(bgWidth, value, bgFillColor, bgLineColor);
			fixButtonsLayout();
		}
		
		/**
		 * 指示窗体是否为最大化模式,调用此函数前必须先将此窗体添加到场景中,否则将引发异常
		 */
		public function set maxMode(value:Boolean):void {
			if (!Boolean(stage)) {
				throw new Error("函数调用序列不正确,在调用此函数前请确保已经将此窗体添加到了场景中");
			}
			
			maxBox.maxMode = value;
			asDragable = !value;
			
			if (value) {
				normalSizeX = this.x;
				normalSizeY = this.y;
				normalSizeWidth = bgWidth;
				normalSizeHeight = bgHeight;
				
				this.x = 0;
				this.y = 0;
				setSize(stage.stageWidth, stage.stageHeight);
			}else {
				this.x = normalSizeX;
				this.y = normalSizeY;
				
				setSize(normalSizeWidth, normalSizeHeight);
			}
		}
		/**
		 * 指示窗体是否为最大化模式
		 */
		public function get maxMode():Boolean {
			return maxBox.maxMode;
		}
		
		/**
		 * 关闭窗体,如果此窗体尚未被添加到场景中,将引发异常
		 */
		public function close():void {
			parent.removeChild(this);
		}
		
		public override  function set bgFillColor(value:uint):void 
		{
			super.bgFillColor = value;
			super.refreshBG();
		}
		
		public override  function set bgEllipseHeight(value:uint):void 
		{
			throw AsmaxErrors.TRY_WRITE_TO_READONLY_PRO;
		}
		
		public override  function set bgEllipseWidth(value:uint):void 
		{
			throw AsmaxErrors.TRY_WRITE_TO_READONLY_PRO;
		}
		
		public override  function set bgHeight(value:Number):void 
		{
			throw AsmaxErrors.TRY_WRITE_TO_READONLY_PRO;
		}
		
		public override  function set bgLineColor(value:uint):void 
		{
			super.bgLineColor = value;
			super.refreshBG();
		}
		
		public override  function set bgWidth(value:Number):void 
		{
			throw AsmaxErrors.TRY_WRITE_TO_READONLY_PRO;
		}
		
		/**
		 * 指示标题栏的颜色
		 */
		public function set titleBarFillColor(value:uint):void {
			_titleBarFillColor = value;
			
			titleBar.bgFillColor = value;
			titleBar.refreshBG();
		}
		/**
		 * 指示标题栏的颜色
		 */
		public function get titleBarFillColor():uint {
			return _titleBarFillColor;
		}
		
		/**
		 * 指示标题栏边框的颜色
		 */
		public function set titleBarLineColor(value:uint):void {
			_titleBarLineColor = value;
			
			titleBar.bgLineColor = value;
			titleBar.refreshBG();
		}
		/**
		 * 指示标题栏边框的颜色
		 */
		public function get titleBarLineColor():uint {
			return _titleBarLineColor;
		}
		
		/**
		 * 定时事件处理器
		 * @param	event
		 */
		private function timerEventHandler(event:TimerEvent):void {
			switch(event.type) {
				case TimerEvent.TIMER:
					notifyBG = !notifyBG;
					break;
				case TimerEvent.TIMER_COMPLETE:
					notifyTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerEventHandler);
					notifyTimer.removeEventListener(TimerEvent.TIMER, timerEventHandler);
					notifyBG = false;
					break;
			}
			
			event.updateAfterEvent();
		}
		
		/**
		 * 指示体是否为通知用户模式
		 */
		private function set notifyBG(value:Boolean):void {
			isNotifyBG = value;
			
			if (isNotifyBG) {
				drawBG(bgWidth, bgHeight, bgFillColor, 0xFFFFFF);
			}else {
				drawBG(bgWidth, bgHeight, bgFillColor, 0xCCCCCC);
			}
		}
		/**
		 * 指示体是否为通知用户模式
		 */
		private function get notifyBG():Boolean {
			return isNotifyBG;
		}
		
		/**
		 * 鼠标事件处理器
		 * @param	event
		 */
		private function mouseEventHandler(event:MouseEvent):void {
			switch(event.type) {
				case MouseEvent.MOUSE_DOWN:
					dragStartMouseX = this.parent.mouseX;
					dragStartMouseY = this.parent.mouseY;
					dragStartThisX = this.x;
					dragStartThisY = this.y;
					
					stage.addEventListener(MouseEvent.MOUSE_UP, mouseEventHandler);
					stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
					break;
				case MouseEvent.MOUSE_UP:
					stage.removeEventListener(MouseEvent.MOUSE_UP, mouseEventHandler);
					stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
					break;
				case MouseEvent.CLICK:
					switch(event.target) {
						case shut:
							if(dispatchEvent(new AFormEvent(AFormEvent.AFORM_CLOSING,false,true))){
								parent.removeChild(this);
								dispatchEvent(new AFormEvent(AFormEvent.AFORM_CLOSE));
							}
							break;
						case maxBox:
							maxMode = !maxMode;
							break;
					}
					break;
				case MouseEvent.DOUBLE_CLICK:
					switch(event.target) {
						case titleBar:
							maxMode = !maxMode;
							break;
					}
					break;
			}
		}
		
		private function mouseMove(event:MouseEvent):void {
			this.x = dragStartThisX + this.parent.mouseX - dragStartMouseX;
			this.y = dragStartThisY + this.parent.mouseY - dragStartMouseY;
			
			event.updateAfterEvent();
		}
		
		/**
		 * 固定按钮布局
		 */
		private function fixButtonsLayout():void {
			switch(true) {
				case isShutable && isMaxable:
					shut.x = titleBar.x + titleBar.width - shut.width - 5;
					shut.y = (titleBar.height - shut.height) / 2 + titleBar.y;
					maxBox.y = (titleBar.height - maxBox.height) / 2 + titleBar.y;
					maxBox.x = shut.x - maxBox.width - 2;
					break;
				case isShutable && !isMaxable:
					shut.x = titleBar.x + titleBar.width - shut.width - 5;
					shut.y = (titleBar.height - shut.height) / 2 + titleBar.y;
					break;
				case !isShutable && isMaxable:
					maxBox.x = titleBar.x + titleBar.width - maxBox.width - 5;
					maxBox.y = (titleBar.height - maxBox.height) / 2 + titleBar.y;
					break;
				case !isShutable && !isMaxable:
					
					break;
			}
		}
		
		/**
		 * 程序指示是否可被拖动
		 */
		private function set asDragable(value:Boolean):void {
			_asDragable = value;
			
			if (dragable) {
				if (!titleBar.hasEventListener(MouseEvent.MOUSE_DOWN)) {
					titleBar.addEventListener(MouseEvent.MOUSE_DOWN, mouseEventHandler);
				}
			}else {
				titleBar.removeEventListener(MouseEvent.MOUSE_DOWN, mouseEventHandler);
			}
		}
		/**
		 * 程序指示是否可被拖动
		 */
		private function get asDragable():Boolean {
			return _asDragable;
		}
		
		/**
		 * 鼠标点击此窗体时将此窗体置于最上层
		 * @param	event
		 */
		private function mouseDownThis(event:MouseEvent):void {
			this.parent.setChildIndex(this, this.parent.numChildren - 1);
		}
	}
	
}